package com.example.wc.logindemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;

/**
 * Created by hui on 2017/2/19.
 */

public class BlogAdapter extends RecyclerView.Adapter <BlogAdapter.BlogHolder> {
    private List<Blog> mBlogs;

    private LayoutInflater layoutInflater;
    private Context mContext;




    public BlogAdapter(Context context, List<Blog> blogs){
        mContext = context;
        mBlogs = blogs;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public BlogHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.blogslist_item, parent, false);
        return new BlogHolder(view);
    }

    @Override
    public void onBindViewHolder(BlogHolder holder, int position) {
        Blog blog = mBlogs.get(position);
        holder.bindCrime(blog);
    }

    @Override
    public int getItemCount() {
        return mBlogs.size();
    }
    
    public void setBlogs(List<Blog> blogs){
        mBlogs = blogs;
    }

//    @Override
//    public void onItemDelete(final int positon) {
//
//        mBlogDelete.setVisibility(View.VISIBLE);
////        VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
////        volleyRequestDemo.volleyRequestDemo_POST_Remove(mContext, mBlog.getId(), new VolleyRequestDemo.BlogPostListener() {
////            @Override
////            public void onBlogPosted() {
////                Log.d("DELETE", "OK");
////                Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
////                mBlogs.remove(positon);
////                notifyDataSetChanged();
////                // TODO: 2017/3/14 刷新BlogsListFragment
////            }
////        });
//
//    }
//
//    @Override
//    public void onMove(int fromPosition, int toPosition) {
//        Collections.swap(mBlogs,fromPosition,toPosition);//交换数据
//        notifyItemMoved(fromPosition,toPosition);
//    }


    class BlogHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private Blog mBlog;
        private TextView mBlogName;
        private TextView mBlogContent;
        private TextView mBlogDelete;
        private LinearLayout mLinearLayout;


        public BlogHolder(View itemView) {
            super(itemView);
            //ViewHolder监听器接口1/2：单个item设置监听器
            itemView.setOnClickListener(this);

            mBlogName = (TextView) itemView.findViewById(R.id.blogs_name);
            mBlogContent = (TextView) itemView.findViewById(R.id.blogs_content);
            mBlogDelete = (TextView) itemView.findViewById(R.id.blog_delete);
            mLinearLayout = (LinearLayout) itemView.findViewById(R.id.blog_item_linear_layout);
        }

        public void bindCrime(final Blog blog){

            mBlog = blog;
            mBlogName.setText(blog.getName());
            mBlogContent.setText(blog.getContent());
            mBlogDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
                    volleyRequestDemo.volleyRequestDemo_POST_Remove(mContext, blog.getId(), new VolleyRequestDemo.BlogPostListener() {
                        @Override
                        public void onBlogPosted() {
                            Log.d("DELETE", "OK");
                            Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
//                            Intent i = new Intent(mContext, BlogsListActivity.class);
//                            mContext.startActivity(i);
                            //notifyItemRemoved(position);
                            mBlogs.remove(blog);
                            mBlogDelete.setVisibility(View.INVISIBLE);
                            mBlogName.setVisibility(View.VISIBLE);
                            mBlogContent.setVisibility(View.VISIBLE);
                            notifyDataSetChanged();
                        }
                    });

                }
            });

        }

        @Override
        public void onClick(View v) {
            //ViewHolder监听器接口2/2：单个item设置监听器，点击跳转到WriteActivity
//            Intent intent = new Intent(mContext, WriteBlogActivity.class);
            //传入2/3:调用WriteActivity.newIntent()方法传递blogId到WriteActivity的Intent中
            Intent intent = EditBlogActivity.newIntent(mContext, mBlog.getId());
            mContext.startActivity(intent);
        }
    }

        public void MyItemTouchHelper(final RecyclerView recyclerView){
            ItemTouchHelper.Callback callback=new ItemTouchHelper.Callback(){

                @Override
                public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                    return makeMovementFlags(ItemTouchHelper.UP|ItemTouchHelper.DOWN,ItemTouchHelper.START|ItemTouchHelper.END );
                }

                @Override
                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                    return true;
                }

                @Override
                public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

                    BlogHolder blogHolder = (BlogHolder) viewHolder;
                    if(direction == 16){
                        blogHolder.mBlogDelete.setVisibility(View.VISIBLE);
//                        FrameLayout.LayoutParams layout = (FrameLayout.LayoutParams)blogHolder.mLinearLayout.getLayoutParams();
//                        layout.setMargins(0, 0 , 100, 0);
//                        blogHolder.mLinearLayout.setLayoutParams(layout);
                        blogHolder.mBlogName.setVisibility(View.INVISIBLE);
                        blogHolder.mBlogContent.setVisibility(View.INVISIBLE);
                        notifyDataSetChanged();
                    }else if(direction ==32){
                        blogHolder.mBlogDelete.setVisibility(View.INVISIBLE);
                        blogHolder.mBlogName.setVisibility(View.VISIBLE);
                        blogHolder.mBlogContent.setVisibility(View.VISIBLE);
                        notifyDataSetChanged();
                    }

//                    clearView(recyclerView, viewHolder);
                }

//                @Override
//                public void onChildDraw(Canvas c, RecyclerView recyclerView,
//                                        RecyclerView.ViewHolder viewHolder, float dX, float dY,
//                                        int actionState, boolean isCurrentlyActive){
////                    if (actionState==ItemTouchHelper.ACTION_STATE_SWIPE){
//
//                        View itemView=viewHolder.itemView;//获取滑动的view
//                        float alpha = 1.0f - Math.abs(dX) / (float) itemView.getWidth();
//                        itemView.setAlpha(alpha);
////                    }
//                }
            };
            ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
            itemTouchHelper.attachToRecyclerView(recyclerView);
        }



}


